import { bg_image } from "./indoorConfig.js";
import {getQrcodeInfo,qrcodeToPhone,fullReductionToPhone,getMerchantInfo,userScan,getUserInfo} from "../api/home/index.js"
export default {
	data() {
		return {
			showOverlay:false,//弹窗
			bg_image,
			currentInputIndex: 0,
			plateArr: ['', '', '', '', '', '', '', ''],
			show: false,
			qrcodeId: null,
			couponMoney: null,
			query: {},
			appoint: 1,
			successMsg: {},
			historyPlate: uni.getStorageSync('history_plate') || []
		}
	},
	async onLoad(options) {
		console.log(options)
		await getUserInfo()
		if (options.merchantQrcodeId) {
			this.qrcodeId = options.merchantQrcodeId
			this._getCoupon()
		}
		
		if (options.appoint) {
			this.appoint = options.appoint
		} else {
			this.appoint = 0
		}
		if (options.params) {
			this.query = JSON.parse(options.params)
		}
	},
	methods: {
		handleShowPlate(index) {
			this.show = true
			console.log(2)
			this.$nextTick(() => {
				this.currentInputIndex = index
			})
		},
		handleIndexChange(index) {
			console.log(1)
			this.currentInputIndex = index
		},
		handleChooseKey(val) {
			this.$set(this.plateArr, this.currentInputIndex, val);
		},
		handleExport(plate) {
			this.show = false
		},
		/**
		 * 领券
		 */
		async _getCoupon() {
			let res = await userScan(this.qrcodeId)
			if(!res) return 
			let couponInfoRes = getQrcodeInfo(this.qrcodeId)
			if(!couponInfoRes) return 
			this.handleSuccess(couponInfoRes.data)
		},
		async sendCoupon() {
			this.show = false;
			//用户扫码领取
			if (this.qrcodeId) {
			} else { //商家直接发送
				let params = {
					plateNumber: this.plateArr.join(''),
				}
				
				if (this.query.qrcodeId === 1) {
					params.qrcodeId = this.query.qrcodeId
					let res = await qrcodeToPhone(params)
					if(res) this.handleSuccess(res.data)
				}else{
					if (this.query.merchantCouponType === 1) {
						params.couponMoney = this.query.couponMoney
					}
					let ress2 = await fullReductionToPhone(params)
					if(!ress2) return
					let datas2 = ress2.data || {}
					this.handleSuccess(datas2)
				}
			}
		},
		handleSuccess(data) {
			if (this.appoint === '1') {
				uni.showToast({
					title: '发送成功',
					icon: 'none'
				})
			} else {
				this.successMsg = data
				this.showOverlay = true
			}
			//记录发送过的车牌到本地
			let plateNumber = this.plateArr.join('')
			//车牌不存在且数量有3个则移除一个
			if (!this.historyPlate.includes(plateNumber) && this.historyPlate.length >= 3) {
				this.historyPlate.shift()
			}
			//不存在的历史车牌才增加记录
			if (!this.historyPlate.includes(plateNumber)) {
				this.historyPlate.push(plateNumber)
			}
			uni.setStorageSync('history_plate', this.historyPlate);
		},
		closeCoupon() {
			this.showOverlay = false
			uni.reLaunch({
				url: `/pages/indoor/home`
			})
		},
		goUse() {
      uni.reLaunch({
				url: `/pages/indoor/parkingRecords`
			})
		},
		handleSelectPlate(plate) {
			this.currentInputIndex = 0
			let _plate = plate.split('')
			if (_plate.length < 8) {
				_plate.push('')
			}
			this.plateArr = [..._plate]
		},
		removeHistoryPlate() {
			this.historyPlate = []
			uni.removeStorageSync('history_plate')
		}
	}
}